package com.hgx.springcloud.orderfeignhystrix.controller;

import com.hgx.springcloud.orderfeignhystrix.feign.IPaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@DefaultProperties(defaultFallback = "globalFallBack")
public class OrderHystrixController {

    @Resource
    private IPaymentHystrixService paymentHystrixService;

    @GetMapping("/order/payment/ok/{id}")
    public String ok(@PathVariable("id") String id) {
        return paymentHystrixService.ok(id);
    }


    @GetMapping("/order/payment/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "timeOutHandler",commandProperties ={
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    } )
    @HystrixCommand
    public String timeout(@PathVariable("id") String id) {
        return paymentHystrixService.timeout(id);
    }


    private String timeOutHandler(String id){
        return "线程："+Thread.currentThread().getName()+"\t"+"客户端80异常，请稍后再试,id:"+id;
    }

    private String globalFallBack(){
        return "线程："+Thread.currentThread().getName()+"\t"+"全局降级处理，请稍后再试";
    }

}
